package com.mitgroup.dmrvs.view;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.mitgroup.dmrvs.R;
import com.mitgroup.dmrvs.utils.AppManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.trinea.android.common.util.PreferencesUtils;

public class MyMainActivity extends AppCompatActivity {
    private ProgressDialog progressDialog;
    private static boolean isExit = false;
    private Intent intent;
    @BindView(R.id.iv_quit)
    public Button quit;
    @OnClick(R.id.iv_quit)
    public void onQuit(View view) {
        new AlertDialog.Builder(this).setTitle("确认退出吗？")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                        PreferencesUtils.putString(MyMainActivity.this,"userId", "");
                        //PreferencesUtils.putString(MyMainActivity.this,"userName", "");
                        // PreferencesUtils.putString(MyMainActivity.this,"password", "");
                        MyMainActivity.this.finish();
                        AppManager.getAppManager().AppExit( MyMainActivity.this);

                    }
                })
                .setNegativeButton("返回", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 点击“返回”后的操作,这里不设置没有任何操作
                    }
                }).show();
    }
    @BindView(R.id.tv_user_name)
    public TextView tvUserName;
    @BindView(R.id.btn_nav01)
    public Button btnReceiveCar;
    @OnClick(R.id.btn_nav01)
    public void onReceiveCar(View view) {
         intent = new Intent(MyMainActivity.this,
                ReceiveCarActivity.class);
        startActivity(intent);
    }
    @BindView(R.id.btn_nav02)
    public Button btnDiagnose;
    @OnClick(R.id.btn_nav02)
    public void onDiagnose(View view) {
        intent = new Intent(MyMainActivity.this,
                CarDiagnoseActivity.class);
        startActivity(intent);
    }
    @BindView(R.id.btn_nav03)
    public Button btnSettings;
    @OnClick(R.id.btn_nav03)
    public void onSettings(View view) {
        intent = new Intent(MyMainActivity.this,
                SettingsActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_main);
        ButterKnife.bind(this);
        initView();
    }
    private void initView() {
        AppManager.getAppManager().addActivity(this);
        String userId = PreferencesUtils.getString(MyMainActivity.this, "userId");
        String userName=PreferencesUtils.getString(MyMainActivity.this,"userName");
        tvUserName.setText(userName);
        //String path= Environment.getExternalStorageDirectory()+"/dmrvs/";
        //Utils.RecursionDeleteFile(new File(path));
    }
    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(MyMainActivity.this, "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            PreferencesUtils.putString(MyMainActivity.this,"userId", "");
            // PreferencesUtils.putString(MyMainActivity.this,"userName", "");
            //PreferencesUtils.putString(MyMainActivity.this,"password", "");
            MyMainActivity.this.finish();
            AppManager.getAppManager().AppExit(this);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity();
    }

}
